package Server;


import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Title: Environment.java</p>
 *
 * <p>Description: Helper class to save all the environment variables that are
 * needed to ensure proper configuration and execution of CGI scripts
 * </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * @Author: YingLuan , Yves Galang
 * @Course: CSC667
 * @Date: 2012 Spring
 *
 * @original author: Luping May and Tracie Hong
 * @version 1.0
 */
public class Environment {

    /**
     * Constructor for Environment object. Saves the information from the server
     * configuration file as well as from the request into a data structure so if
     * the request is for a cgi script, it can be executed properly.
     *
     * @param c HttpdConf file which contains some variables which need to be
     *   saved in order for cgi scripts to execute properly.
     * @param r Request object which contains request specific variables needed
     *   to execute cgi scripts
     * @param cl Socket used by the client to connect to the server
     */
    public Environment(HttpdConf c, Request r, Socket cl) {
        conf = c;
        req = r;
        socket = cl;
    }

    /**
     * Function to save static variables into your data structure which can be
     * then used during script execution. Variables that can also be accessed
     * directly from the request object or the socket (objects passed in during
     * instantiation) can be saved here as well.
     * HINT: Most request variables that need to be saved need to have an HTTP_
     * variable name so take this into consideration when planning how to design
     * and save environment variables.
     */
    public void setEnv(Map<String, String> set) {
        env = set;
        HashMap<String, String> mHeaders = req.getHeader();
        env.put("SERVER_SOFTWARE", "TEST" + "/1.0");
        env.put("SERVER_NAME", "TEST");
        for (String key : mHeaders.keySet()) {
            env.put("HTTP_" + key.toUpperCase(), mHeaders.get(key));
        }
        env.put("GATEWAY_INTERFACE", "CGI");
        env.put("SERVER_PROTOCOL", "HTTP/" + req.getHttp_ver());
        env.put("SERVER_PORT", conf.getSettings("Listen"));
        env.put("REQUEST_METHOD", req.getMethod());
        env.put("PATH_INFO", req.getRequest_uri());
        env.put("PATH_TRANSLATED", conf.solveAlias(req.getRequest_uri()));
        env.put("QUERY_STRING", req.getQuery());
        env.put("REMOTE_HOST", socket.getInetAddress().getCanonicalHostName());
        env.put("REMOTE_ADDR", socket.getInetAddress().getHostAddress());
        env.put("REMOTE_USER", socket.getInetAddress().getHostName());
        HashMap header = req.getHeader();
        if (header.containsKey("Content-Length")) {
            env.put("CONTENT_LENGTH", (String) header.get("Content-Length"));
        }
        if (header.containsKey("Content-Type")) {
            env.put("CONTENT_TYPE", (String) header.get("Content-Type"));
        }
    }

    /**
     * Function to return a data structure that contains all the environment
     * variables that were saved.
     *
     * @return String[]. Chose as data type upon return due to parsing of variable
     *   content and name which both need to be stored.
     */
//  public String[] getEnv() {
    // }
    /**
     * Used to add another variable to the data structure. Usually used when the
     * data has not be stored yet through the setEnv function.
     *
     * @param var String that contains both the variable name and data. Used this
     *   way for parsing purposes later on.
     */
    public void addEnvVar(String key, String value) {
        env.put(key, value);
    }
    private Map<String, String> env;
    private HttpdConf conf;
    private Request req;
    private Socket socket;
}